# vim:set ft= ts=4 sw=4 et fdm=marker:

use Test::Nginx::Socket::Lua;
use Dotenv -load => 't/.env';


plan tests => repeat_each() * (blocks() * 3);


$ENV{TEST_NGINX_REDIS_HOST}     ||= '127.0.0.1';
$ENV{TEST_NGINX_REDIS_PORT}     ||= 6379;
$ENV{TEST_NGINX_REDIS_PASSWORD} ||= '';
$ENV{TEST_NGINX_REDIS_DB}       ||= 0;

our $http_config = <<'_EOC_';
    lua_package_path "${prefix}../../script/?.ljbc;${prefix}../../script/?.lua;;";
_EOC_

no_shuffle();
no_long_string();

run_tests();

__DATA__


=== TEST Casino.PeekTransfer
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Casino.PeekTransfer.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Casino.PeekTransfer(gaas
                                        , "onepiece"       -- provider
                                        , "TRANS-000115"   -- transaction_id
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"amount":"-20.00","asset":"160.00","balance":"140.00000","casino":"FISH_ISLAND","casino_transaction_id":"TRANS-000115","commit_at":1577301129,"currency":"USD","dealer":"PP","dealer_player":"8390384","non_expendable":"20.00","operation_urn":"OP-TRANS-000115","player":"luffy","provider":"onepiece","status":"FULFILLED","transaction_idempotence":"37","transfer_id":"1912250000000000001","wallet":"wallet_us"}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.CancelBet
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.CancelBet.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.CancelBet(gaas
                                        , nil                     -- server
                                        , 'session-12345'         -- session
                                        , 'thelostworld'          -- game
                                        , '2005260000000000001'   -- ticket_id
                                        , 'vvvvc2f5d'             -- operation_urn
                                        , 1590502289              -- timestamp
                                        , false                   -- forcible
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"_idempotence":2,"amount":"12.50000","asset":"200.00000","balance":"160.00000","non_expendable":"40.00000","receipt_id":"2005260000000000002"}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.PeekBet (1)
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.PeekBet.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.PeekBet(gaas
                                        , '2005260000000000001'  -- ticket_id
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"account":"990505","account_type":"vip","bet/transaction_amount":"-12.50000","bet/transaction_at":1590502289,"bet/transaction_balance":"147.50000","bet/transaction_id":"2005260000000000001","bet/transaction_idempotence":12,"casino":"straw_hat","country":"GB","currency":"USD","dealer":"WT","dealer_player":"wt_luffy","game":"thelostworld","leader_ticket":"200000000001","player":"luffy","player_device":"browser","player_ip":"60.134.56.4","player_os":"windows","provider":"onepiece","room":"01","round_type":"FREE_BONUS","session":"session-12345","stage":"45123","status":"RUNNING","ticket_id":"2005260000000000001","timezone":"CST","triggered_ticket":"200000000001","wager":"12.50000","wallet":"wallet_us"}}
--- error_code: 200
--- no_error_log
[error]


=== TEST Fish.PeekBet (2)
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.PeekBet.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.PeekBet(gaas
                                        , '2005260000000000003'  -- ticket_id
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"account":"990505","account_type":"vip","bet/transaction_amount":"-12.50000","bet/transaction_at":1590502289,"bet/transaction_balance":"147.50000","bet/transaction_id":"2005260000000000003","bet/transaction_idempotence":84,"bonus":{"bonusOdds":"20","bonusType":"2","bonusWin":"4000"},"casino":"straw_hat","country":"GB","currency":"USD","dealer":"WT","dealer_player":"wt_luffy","game":"thelostworld","leader_ticket":"200000000002","player":"luffy","player_device":"browser","player_ip":"60.134.56.4","player_os":"windows","provider":"onepiece","room":"01","round_type":"FREE_BONUS","server":"tfgs002-gameserver.local.inaddr","session":"session-34345","settle/transaction_amount":"1.50000","settle/transaction_at":1590502289,"settle/transaction_balance":"148.50000","settle/transaction_id":"2005260000000000004","settle/transaction_idempotence":85,"stage":"45123","status":"SETTLED","symbol":["30008","20005"],"ticket_id":"2005260000000000003","timezone":"CST","triggered_ticket":"200000000002","wager":"12.50000","wallet":"wallet_us","win_type":"WIN","winlost":"1.00000"}}
--- error_code: 200
--- no_error_log
[error]


=== TEST Fish.PlaceBet
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.PlaceBet.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.PlaceBet(gaas
                                        , nil              -- server
                                        , 'session-12345'  -- session
                                        , 'thelostworld'   -- game
                                        , '01'             -- room
                                        , nil              -- round
                                        , 'onepiece'       -- provider
                                        , 'luffy'          -- player
                                        , 'wallet_us'      -- wallet
                                        , 12.5             -- wager
                                        , 45123            -- stage
                                        , 'FREE_BONUS'     -- stage_type
                                        , '990505'         -- account
                                        , 'vip'            -- account_type
                                        , 'WT'             -- dealer
                                        , 'wt_luffy'       -- dealer_player
                                        , 'straw_hat'      -- casino
                                        , 'GB'             -- country
                                        , 'CST'            -- timezone
                                        , 'browser'        -- player_device
                                        , 'windows'        -- player_os
                                        , '60.134.56.4'    -- player_ip
                                        , '56f5ecfc2f5d'   -- operation_urn
                                        , 1590502289       -- timestamp
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"_idempotence":2,"asset":"200.00000","balance":"147.50000","non_expendable":"52.50000","receipt_id":"2005260000000000001"}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.SettleBet (1)
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.SettleBet.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.SettleBet(gaas
                                        , nil                    -- server
                                        , 'session-12345'        -- session
                                        , 'thelostworld'         -- game
                                        , '2005260000000000001'  -- transaction_id
                                        , 35.75                  -- winlost
                                        , nil                    -- win_type
                                        , nil                    -- bonus
                                        , nil                    -- symbol 
                                        , nil                    -- stage
                                        , nil                    -- stage_type
                                        , nil                    -- triggered_stage
                                        , nil                    -- triggered_stage_type
                                        , 'ffffc2f5d'            -- operation_urn
                                        , 1590502289             -- timestamp
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"_idempotence":3,"asset":"223.25000","balance":"170.75000","non_expendable":"52.50000","receipt_id":"2005260000000000003"}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.SettleBet (2)
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.SettleBet.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.SettleBet(gaas
                                        , nil                    -- server
                                        , 'session-12345'        -- session
                                        , 'thelostworld'         -- game
                                        , '2005260000000000002'  -- transaction_id
                                        , 15.75                  -- winlost
                                        , nil                    -- win_type
                                        , {                      -- bonus
                                            bonusOdds = "20",
                                            bonusWin  = "4000",
                                            bonusType = "2"
                                          }
                                        , {                      -- symbol
                                            "30008",
                                            "20005"
                                          }
                                        , 45224                  -- stage
                                        , nil                    -- stage_type
                                        , 9945224                -- triggered_stage
                                        , 'FREE_BONUS'           -- triggered_stage_type
                                        , 'ffffc2fxx'            -- operation_urn
                                        , 1590502289             -- timestamp
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"_idempotence":3,"asset":"203.25000","balance":"150.75000","non_expendable":"52.50000","receipt_id":"2005260000000000003"}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.SettleBonus
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.SettleBonus.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.SettleBonus(gaas
                                        , nil              -- server
                                        , 'session-12345'  -- session
                                        , 'thelostworld'   -- game
                                        , '01'             -- room
                                        , 'onepiece'       -- provider
                                        , 'luffy'          -- player
                                        , 'wallet_us'      -- wallet
                                        , 37.75            -- winlost
                                        , 'WIN'            -- win_type
                                        , {                      -- bonus
                                            bonusOdds = "20",
                                            bonusWin  = "4000",
                                            bonusType = "2"
                                          }
                                        , {                      -- symbol
                                            "30008",
                                            "20005"
                                          }
                                        , 45123            -- stage
                                        , 'FREE_BONUS'     -- stage_type
                                        , nil              -- triggered_stage
                                        , nil              -- triggered_stage_type
                                        , '990505'         -- account
                                        , 'vip'            -- account_type
                                        , 'WT'             -- dealer
                                        , 'wt_luffy'       -- dealer_player
                                        , 'straw_hat'      -- casino
                                        , 'GB'             -- country
                                        , 'CST'            -- timezone
                                        , 'browser'        -- player_device
                                        , 'windows'        -- player_os
                                        , '60.134.56.4'    -- player_ip
                                        , '56f5ecfc2f5d'   -- operation_urn
                                        , 1590502289       -- timestamp
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"_idempotence":3,"asset":"237.75000","balance":"197.75000","non_expendable":"40.00000","receipt_id":"2005260000000000002","ticket_id":"2005260000000000001"}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.InitSessionState
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.InitSessionState.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.InitSessionState(gaas
                                        , nil                   -- server
                                        , 'session-12345'       -- session
                                        , 'thelostworld'        -- game
                                        , nil                   -- room
                                        , nil                   -- round
                                        , 'onepiece'            -- provider
                                        , 'luffy'               -- player
                                        , '**DISPOSABLE_WALLET' -- wallet
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":true}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.KeepSessionState
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.KeepSessionState.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.KeepSessionState(gaas
                                        , nil                   -- server
                                        , 'session-12345'       -- session
                                        , 'onepiece'            -- provider
                                        , 'luffy'               -- player
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":true}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.PeekSessionState
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.PeekSessionState.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.PeekSessionState(gaas
                                        , nil                   -- server
                                        , 'session-12345'       -- session
                                        , 'onepiece'            -- provider
                                        , 'luffy'               -- player
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"counter/fish/bet":0,"counter/fish/bet/round_type:BOSS":0,"counter/fish/bet/round_type:FREE_BONUS":0,"counter/fish/bet/round_type:NORMAL":0,"counter/fish/bet_status:CANCELLED":0,"counter/fish/bet_status:RUNNING":0,"game":"thelostworld","metric/fish/turnover":"0","player_session":"R210430000000234","session":"session-12345","transfer_idempotence":0,"wallet":"session-12345"}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.DeinitStage
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.DeinitStage.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.DeinitStage(gaas
                                        , nil              -- server
                                        , 'session-12345'  -- session
                                        , 'thelostworld'   -- game
                                        , '01'             -- room
                                        , 1274399          -- stage
                                        , 1                -- bet_count
                                        , 1590502296       -- timestamp
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"begin_at":1590502289,"bet_count":"1","end_at":1590502296,"obtained_by":"BS201000000012","parent":"0","player":"luffy","provider":"onepiece","type":"FREE_BONUS"}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.InitStage (1)
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.InitStage.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.InitStage(gaas
                                        , nil               -- server
                                        , 'session-12345'   -- session
                                        , 'thelostworld'    -- game
                                        , nil               -- room
                                        , 10                -- stage
                                        , 'FREE_BONUS'      -- stage_type
                                        , 0                 -- parent_stage
                                        , nil               -- obtained_by
                                        , 1590502289        -- timestamp
                                        , true              -- forcible
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":true}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.InitStage (2)
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.InitStage.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.InitStage(gaas
                                        , nil               -- server
                                        , 'session-12345'   -- session
                                        , 'thelostworld'    -- game
                                        , nil               -- room
                                        , 1274399           -- stage
                                        , 'FREE_BONUS'      -- stage_type
                                        , 0                 -- parent_stage
                                        , 'BS201000000012'  -- obtained_by
                                        , 1590502289        -- timestamp
                                        , true              -- forcible
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":true}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.PeekStage
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.PeekStage.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.PeekStage(gaas
                                        , nil               -- server
                                        , 'session-12345'   -- session
                                        , 'thelostworld'    -- game
                                        , '01'              -- room
                                        , 1274399           -- stage
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"begin_at":1590502289,"obtained_by":"BS201000000012","parent":"0","player":"luffy","provider":"onepiece","type":"FREE_BONUS"}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.AcceptTransfer
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.AcceptTransfer.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.AcceptTransfer(gaas
                                        , '1912259000000000001'  -- transfer_id
                                        , nil                    -- amount
                                        , 'TRANS-000115'         -- casino_transaction_id
                                        , 1590502289             -- timestamp
                                        , false                  -- forcible
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"amount":"-20.00000","balance":"0.00000","casino":"fish_island","casino_transaction_id":"TRANS-000115","commit_at":1590502289,"currency":"USD","dealer":"PP","dealer_player":"8390384","fulfill/transaction_balance":"0.00000","fulfill/transaction_id":"2005269000000000004","game":"thelostworld","operation_urn":"OP-TRANS-000115","place/transaction_balance":"140.00000","place/transaction_id":"1912259000000000001","place_at":1577301129,"player":"luffy","provider":"onepiece","review_at":1590502289,"server":"tfgs002-gameserver.local.inaddr","session":"session-12345","stage/reviewing_id":"1629792344663-0","stage/uncommitted_id":"1629168328832-0","status":"FULFILLED","transaction_idempotence":2,"transfer_id":"1912259000000000001","wallet":"wallet_us"}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.CancelTransfer
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.CancelTransfer.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.CancelTransfer(gaas
                                        , '1912259000000000001'     -- transfer_id
                                        , 'TRANS-000115-CANCEL'     -- casino_transaction_id
                                        , 'OP-TRANS-000115-CANCEL'  -- operation_urn,
                                        , 1590502289                -- timestamp
                                        , false                     -- forcible
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"refund":{"amount":"20.00000","asset":"200.00000","balance":"20.00000","casino":"fish_island","casino_transaction_id":"TRANS-000115-CANCEL","commit_at":1590502289,"currency":"USD","dealer":"PP","dealer_player":"8390384","game":"thelostworld","non_expendable":"180.00000","operation_urn":"OP-TRANS-000115-CANCEL","player":"luffy","provider":"onepiece","refund_for":"1912259000000000001","server":"tfgs002-gameserver.local.inaddr","session":"session-12345","status":"FULFILLED","transaction_idempotence":5,"transfer_id":"2005269000000000004","wallet":"wallet_us"},"transfer":{"amount":"-20.00000","cancel/transaction_balance":"20.00000","cancel/transaction_id":"2005269000000000004","cancel_at":1590502289,"casino":"fish_island","casino_transaction_id":"TRANS-000115-CANCEL","currency":"USD","dealer":"PP","dealer_player":"8390384","game":"thelostworld","operation_urn":"OP-TRANS-000115","place/transaction_balance":"140.00000","place/transaction_id":"1912259000000000001","place_at":1577301129,"player":"luffy","provider":"onepiece","server":"tfgs002-gameserver.local.inaddr","session":"session-12345","stage/uncommitted_id":"1629168328832-0","status":"CANCELLED","transaction_idempotence":2,"transfer_id":"1912259000000000001","wallet":"wallet_us"}}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.CommitTransfer
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.CommitTransfer.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.CommitTransfer(gaas
                                        , nil                -- server
                                        , 'session-1234'     -- session
                                        , 'xmas'             -- game
                                        , 'onepiece'         -- provider
                                        , 'luffy'            -- player
                                        , 'wallet_us'        -- wallet
                                        , 'USD'              -- currency
                                        , '-20.0'            -- amount
                                        , nil                -- dealer
                                        , nil                -- dealer_player
                                        , nil                -- casino
                                        , nil                -- casino_transaction_id
                                        , 'OP-TRANS-000115'  -- operation_urn
                                        , 1577301129         -- timestamp
                                        , false              -- forcible
                                        , false              -- dismiss_wallet
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"amount":"-20.0","asset":"180.00000","balance":"140.00000","commit_at":1577301129,"currency":"USD","game":"xmas","non_expendable":"40.00000","operation_urn":"OP-TRANS-000115","player":"luffy","provider":"onepiece","session":"session-1234","status":"FULFILLED","transaction_idempotence":2,"transfer_id":"1912259000000000001","wallet":"wallet_us"}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.FulfillTransfer
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.FulfillTransfer.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.FulfillTransfer(gaas
                                        , '1912259000000000001'  -- transfer_id
                                        , nil                    -- amount
                                        , 'TRANS-000115'         -- casino_transaction_id
                                        , 1590502289             -- timestamp
                                        , false                  -- forcible
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"amount":"-20.00000","balance":"0.00000","casino":"fish_island","casino_transaction_id":"TRANS-000115","commit_at":1590502289,"currency":"USD","dealer":"PP","dealer_player":"8390384","fulfill/transaction_balance":"0.00000","fulfill/transaction_id":"2005269000000000004","game":"thelostworld","operation_urn":"OP-TRANS-000115","place/transaction_balance":"140.00000","place/transaction_id":"1912259000000000001","place_at":1577301129,"player":"luffy","provider":"onepiece","server":"tfgs002-gameserver.local.inaddr","session":"session-12345","stage/uncommitted_id":"1629168328832-0","status":"FULFILLED","transaction_idempotence":2,"transfer_id":"1912259000000000001","wallet":"wallet_us"}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.PlaceTransfer
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.PlaceTransfer.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.PlaceTransfer(gaas
                                        , 'tfgs002-gameserver.local.inaddr'  -- server
                                        , 'session-12345'                    -- session
                                        , 'thelostworld'                     -- game
                                        , 'onepiece'                         -- provider
                                        , 'luffy'                            -- player
                                        , 'wallet_us'                        -- wallet
                                        , 'USD'                              -- currency
                                        , '-20.0'                            -- amount
                                        , 'PP'                               -- dealer
                                        , '8390384'                          -- dealer_player
                                        , 'fish_island'                      -- casino
                                        , 'OP-TRANS-000115'                  -- operation_urn
                                        , 1577301129                         -- timestamp
                                        , false                              -- forcible
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body_like chomp
^\{"res":\{"amount":"-20.00000","balance":"140.00000","casino":"fish_island","currency":"USD","dealer":"PP","dealer_player":"8390384","game":"thelostworld","operation_urn":"OP-TRANS-000115","place\/transaction_balance":"140.00000","place\/transaction_id":"1912259000000000001","place_at":1577301129,"player":"luffy","player_session":"R210430000000234","provider":"onepiece","server":"tfgs002-gameserver.local.inaddr","session":"session-12345","session_transfer_idempotence":1,"stage\/uncommitted_id":"\d+-\d+","status":"PLACED","transaction_idempotence":2,"transfer_id":"1912259000000000001","wallet":"wallet_us"}}$
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.RejectTransfer
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.RejectTransfer.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.RejectTransfer(gaas
                                        , '1912259000000000001'     -- transfer_id
                                        , 'TRANS-000115-CANCEL'     -- casino_transaction_id
                                        , 'OP-TRANS-000115-CANCEL'  -- operation_urn
                                        , 1590502293                -- timestamp
                                        , false                     -- forcible
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"refund":{"amount":"20.00000","asset":"200.00000","balance":"20.00000","casino":"fish_island","casino_transaction_id":"TRANS-000115-CANCEL","commit_at":1590502293,"currency":"USD","dealer":"PP","dealer_player":"8390384","game":"thelostworld","non_expendable":"180.00000","operation_urn":"OP-TRANS-000115-CANCEL","player":"luffy","provider":"onepiece","refund_for":"1912259000000000001","server":"tfgs002-gameserver.local.inaddr","session":"session-12345","status":"FULFILLED","transaction_idempotence":5,"transfer_id":"2005269000000000004","wallet":"wallet_us"},"transfer":{"amount":"-20.00000","cancel/transaction_balance":"20.00000","cancel/transaction_id":"2005269000000000004","cancel_at":1590502293,"casino":"fish_island","casino_transaction_id":"TRANS-000115-CANCEL","currency":"USD","dealer":"PP","dealer_player":"8390384","game":"thelostworld","operation_urn":"OP-TRANS-000115","place/transaction_balance":"140.00000","place/transaction_id":"1912259000000000001","place_at":1577301129,"player":"luffy","provider":"onepiece","review_at":1590502289,"server":"tfgs002-gameserver.local.inaddr","session":"session-12345","stage/reviewing_id":"1629792344663-0","stage/uncommitted_id":"1629168328832-0","status":"CANCELLED","transaction_idempotence":2,"transfer_id":"1912259000000000001","wallet":"wallet_us"}}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Fish.ReviewTransfer
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Fish.ReviewTransfer.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Fish.ReviewTransfer(gaas
                                        , '1912259000000000001'     -- transfer_id
                                        , 1590502289                -- timestamp
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body_like chomp
^\{"res":\{"amount":"-20.00000","balance":"140.00000","casino":"fish_island","currency":"USD","dealer":"PP","dealer_player":"8390384","game":"thelostworld","operation_urn":"OP-TRANS-000115","place\/transaction_balance":"140.00000","place/transaction_id":"1912259000000000001","place_at":1577301129,"player":"luffy","provider":"onepiece","review_at":1590502289,"server":"tfgs002-gameserver.local.inaddr","session":"session-12345","stage\/reviewing_id":"\d+-\d+","stage\/uncommitted_id":"1629168328832-0","status":"PENDING","transaction_idempotence":2,"transfer_id":"1912259000000000001","wallet":"wallet_us"}}$
--- error_code: 200
--- no_error_log
[error]



=== TEST Player.GrantPermission (1)
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Player.GrantPermission.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Player.GrantPermission(gaas
                                        , 'onepiece'             -- provider
                                        , 'luffy'                -- player
                                        , 'WALLET.TRANSFER_OUT'  -- permission
                                        , nil                    -- privilege
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":false}
--- error_code: 200
--- no_error_log
[error]



=== TEST Player.GrantPermission (2)
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Player.GrantPermission.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Player.GrantPermission(gaas
                                        , 'onepiece'             -- provider
                                        , 'luffy'                -- player
                                        , 'WALLET.TRANSFER_OUT'  -- permission
                                        , 'for_commit()'         -- privilege
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":true}
--- error_code: 200
--- no_error_log
[error]



=== TEST Player.ListPermission
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Player.ListPermission.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Player.ListPermission(gaas
                                        , 'onepiece'   -- provider
                                        , 'luffy'      -- player
                                        , nil          -- privilege
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"WALLET.TRANSFER_IN":"allow","WALLET.TRANSFER_OUT":"allow"}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Player.RevokePermission
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Player.RevokePermission.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Player.RevokePermission(gaas
                                        , 'onepiece'              -- provider
                                        , 'luffy'                 -- player
                                        , 'WALLET.TRANSFER_OUT'   -- permission
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":true}
--- error_code: 200
--- no_error_log
[error]



=== TEST Player.CommitTransfer
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Player.CommitTransfer.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Player.CommitTransfer(gaas
                                        , nil                -- server
                                        , nil                -- session
                                        , nil                -- game
                                        , 'onepiece'         -- provider
                                        , 'luffy'            -- player
                                        , 'wallet_us'        -- wallet
                                        , 'USD'              -- currency
                                        , '-20.0'            -- amount
                                        , nil                -- dealer
                                        , nil                -- dealer_player
                                        , nil                -- casino
                                        , nil                -- casino_transaction_id
                                        , 'OP-TRANS-000115'  -- operation_urn
                                        , 1577301129         -- timestamp
                                        , nil                -- forcible
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"amount":"-20.0","asset":"180.00000","balance":"140.00000","commit_at":1577301129,"currency":"USD","non_expendable":"40.00000","operation_urn":"OP-TRANS-000115","player":"luffy","provider":"onepiece","status":"FULFILLED","transaction_idempotence":2,"transfer_id":"1912250000000000001","wallet":"wallet_us"}}
--- error_code: 200
--- no_error_log
[error]



=== TEST Player.LookupTransfer
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Player.LookupTransfer.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Player.LookupTransfer(gaas
                                        , 'onepiece'   -- provider
                                        , 'luffy'      -- player
                                        , nil          -- count
                                        , nil          -- status
                                        , nil          -- currency
                                        , nil          -- wallet
                                        , nil          -- transaction_id
                                        , nil          -- transfer_id
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":[{"amount":"220.00","asset":"260.00000","balance":"220.00000","casino":"FISH_ISLAND","casino_transaction_id":"TRANS-GAME-000335","commit_at":1577301129,"currency":"USD","dealer":"PP","dealer_player":"8390384","game":"FortunateFish2","non_expendable":"40.00000","operation_urn":"OP-TRANS-GAME-000335","player":"luffy","provider":"onepiece","server":"tfgs002-gameserver.local.inaddr","session":"session-34345","status":"FULFILLED","transaction_idempotence":"105","transfer_id":"1912250000000000005","wallet":"wallet_us"},{"amount":"250.00","asset":"930.00","balance":"390.00000","casino":"FISH_ISLAND","casino_transaction_id":"TRANS-000145","commit_at":1577421909,"currency":"USD","dealer":"PP","dealer_player":"8390384","non_expendable":"540.00","operation_urn":"OP-TRANS-000145","player":"luffy","provider":"onepiece","status":"FULFILLED","transaction_idempotence":"88","transfer_id":"1912270000000000002","wallet":"wallet_us"},{"amount":"-20.00","asset":"160.00","balance":"140.00000","casino":"FISH_ISLAND","casino_transaction_id":"TRANS-000115","commit_at":1577301129,"currency":"USD","dealer":"PP","dealer_player":"8390384","non_expendable":"20.00","operation_urn":"OP-TRANS-000115","player":"luffy","provider":"onepiece","status":"FULFILLED","transaction_idempotence":"37","transfer_id":"1912250000000000001","wallet":"wallet_us"}]}
--- error_code: 200
--- no_error_log
[error]



=== TEST Player.ListWallet
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Player.ListWallet.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Player.ListWallet(gaas
                                        , 'onepiece'   -- provider
                                        , 'luffy'      -- player
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":[{"balance":"160.00000","currency":"USD","status":"ACTIVATED","wallet":"wallet_us"},{"balance":"3070.00000","currency":"CNY","status":"DEACTIVATED","wallet":"wallet_cn"}]}
--- error_code: 200
--- no_error_log
[error]



=== TEST Player.PeekWallet
--- http_config eval: $::http_config
--- config
        root "";
        set $TESTCASE_FILE   "${realpath_root}/../testcase/script.gaas.Player.PeekWallet.testdata";

        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local repo  = require("shared.api.gaas-state-provider")
                local gaas  = repo.create_repository(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    require("shared.test.redis-test").setup_test_case(redis, ngx.var.TESTCASE_FILE)
                end

                -- perform response
                do
                    local ljson = require "shared.test.ljson"

                    local res, err = repo.Player.PeekWallet(gaas
                                        , 'onepiece'    -- provider
                                        , 'luffy'       -- player
                                        , 'wallet_us'   -- wallet
                                        )

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, 0, 1, 2, 3)
                end
            }
        }
--- request
GET /t
--- response_body chomp
{"res":{"_idempotence":1,"asset":"200.00000","balance":"160.00000","currency":"USD","non_expendable":"40.00000","status":"ACTIVATED","wallet":"wallet_us"}}
--- error_code: 200
--- no_error_log
[error]


